<?php

namespace StudyBuddy\Controllers;

use \StudyBuddy\WebPage;

/**
 * Controller for
 *
 */
class Settings extends WebPage {

    protected $membersOnly = true;
    protected $layoutID = 1;

    protected function main() {
        /**
         * Must reload viewer object
         * to pickup the current role
         * in case user activated account from
         * different browser
         */
        $this->oRegistry->Viewer->reload();

        $this->aPageVars['title'] = $this->_('Edit settings');

        $vals = array(
            'confirmation' => $this->makeConfirmBlock(),
            'change_password' => $this->_('Change Password'),
            'profile' => $this->_('Edit Profile'),
            'profileUrl' => '/editprofile/',
            'emailPrefs' => $this->_('Email Preferences'),
            'clearCache' => '',
            'listFlag' => ''
        );

        if ($this->oRegistry->Viewer->isAdmin()) {
            $vals['clearCache'] = $this->makeClearCache();
            $vals['listFlag'] = $this->makeListFlag();
        }

        $this->aPageVars['body'] = \tplSettings::parse($vals);
    }

    protected function makeClearCache() {
        return '<div class="tool"> 
			<div class="icn sweep">&nbsp;</div> 
			<div class="tool_link"><a href="/clearcache/" class="ajax clearcache">' . $this->_('Clear Cache') . '</a></div> 
		</div>';
    }

    protected function makeListFlag() {
        return '<div class="tool"> 
					<div class="icn flag">&nbsp;</div> 
					<div class="tool_link"><a href="/listflag/" class="ajax listflag">' . $this->_('List Flags') . '</a></div> 
				</div>';
    }

    protected function makeConfirmBlock() {
        $role = $this->oRegistry->Viewer->getRoleId();
        $email = $this->oRegistry->Viewer->email;
        if (strstr($role, 'unactivated')) {
            return \tplConfirmemail::parse(array('email' => $email));
        }

        return '';
    }

}